#include <iostream>
#include <fstream>
#include "contacts.pb.h"

void AddPeopleInfo(contacts::PeopleInfo *people)
{
    std::cout << "-------新增联系人-------" << std::endl;

    std::cout << "请输入姓名：" << std::endl;
    std::string name;
    getline(std::cin, name);
    people->set_name(name);

    std::cout << "请输入年龄：" << std::endl;
    int age;
    std::cin >> age;
    people->set_age(age);
    // std::cin.ignore() 是 istream 类的成员函数，用于从输入流中提取并丢弃指定数量的字符，直到遇到特定的终止字符。
    std::cin.ignore(256, '\n');

    // 添加电话号，对数组操作
    for (int i = 0;; i++)
    {
        std::cout << "请输入联系人电话：" << std::endl;
        std::string number;
        getline(std::cin, number);
        if (number.empty())
        {
            break;
        }
        contacts::PeopleInfo_Phone *phone = people->add_phone();
        phone->set_number(number);
        std::cout << "请输入电话类型（1、移动电话  2、固定电话）" << std::endl;
        int type;
        std::cin >> type;
        std::cin.ignore(256, '\n');
        switch (type)
        {
        case 1:
            phone->set_type(contacts::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_MP);
            break;
        case 2:
            phone->set_type(contacts::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_TEL);
            break;
        default:
            std::cout << "电话类型错误" << std::endl;
            break;
        }
    }

    contacts::Address adderss;

    std::cout<<"请输入联系人家庭地址：";
    std::string home_add;
    getline(std::cin, home_add);
    adderss.set_home_add(home_add);

    std::cout<<"请输入联系人单位地址：";
    std::string unit_add;
    getline(std::cin, unit_add);
    adderss.set_unit_add(unit_add);

    // Address -> Any
    people->mutable_data()->PackFrom(adderss);

    std::cout<<"请选择要添加的其他联系方式(1. qq   2. wechat): ";
    int other_contanct;
    std::cin>>other_contanct;
    std::cin.ignore(256, '\n');
    if(1 == other_contanct)
    {
        std::cout<<"请输入qq: ";
        std::string qq;
        getline(std::cin, qq);
        people->set_qq(qq);
    }
    else if(2 == other_contanct)
    {
        std::cout<<"请输入wechat: ";
        std::string wechat;
        getline(std::cin, wechat);
        people->set_wechat(wechat);
    }
    else
    {
        std::cout<<"选择有误！未成功设置其他联系方式";
    }

    for(int i = 0; ;i++)
    {
        std::cout<<"请输入备注"<<i+1<<"标题(只输入回车完成备注): ";
        std::string remark_key;
        getline(std::cin, remark_key);
        if(remark_key.empty())
        {
            break;
        }
        std::cout<<"请输入备注"<<i+1<<"内容: ";
        std::string remark_val;
        getline(std::cin, remark_val);
        people->mutable_remark()->insert({remark_key, remark_val});
    }

    std::cout << "联系人添加成功！" << std::endl;
}

int main()
{
    contacts::Contacts con;
    // 读取已经存在的通讯录文件
    std::fstream input("con.bin", std::ios::in | std::ios::binary);
    if (!input)
    {
        std::cerr << "con.bin文件不存在，创建新文件！" << std::endl;
    }
    else if (!con.ParseFromIstream(&input))
    {
        std::cerr << "读取失败!" << std::endl;
        input.close();
        return -1;
    }

    // 向通讯录中添加一个联系人
    contacts::PeopleInfo *people = con.add_people();
    AddPeopleInfo(people);

    // 将通讯录写入文件
    std::fstream output("con.bin", std::ios::binary | std::ios::out | std::ios::trunc);
    if (!con.SerializePartialToOstream(&output))
    {
        std::cerr << "写入失败！" << std::endl;
        input.close();
        output.close();
        return -1;
    }
    std::cout << "写入成功！" << std::endl;
    input.close();
    output.close();

    return 0;
}